//index.js
import config from '../../../config.js';
const base = config;
//获取应用实例
const app = getApp();
Page({
  data: {
    mainInfo: {},
    // banner轮播
    swiper: {
      autoplay: true,
      indicatorDots: true,
      url: 'http://www.baidu.com',
    },
    menus: [{
        url: "../gjorder/gjorders",
        ico: "../../../image/guanjia/order.png",
        name: "订单管理"
      },
      {
        url: "../placeOrder/placeOrder",
        ico: "../../../image/guanjia/order_delegate.png",
        name: "代客下单"
      },
      {
        url: "../customerManage/customerManage",
        ico: "../../../image/guanjia/custom.png",
        name: "客户管理"
      },
      {
        url: "../comment/comment",
        ico: "../../../image/guanjia/comment.png",
        name: "用户评价"
      }

    ],

    // 事故提示
    tips: {
      autoplay: true,
      indicatorDots: false,
      vertical: true,
      circular: true,
      list: []
    },

    // 功能列表
    funList: [{
      icon: "../../image/default.png",
      text: "车险报价",
      url: "../insurance/insurance"
    }, {
      icon: "../../image/default.png",
      text: "事故协助",
      url: "../accidentAssi/accidentAssi"
    }, {
      icon: "../../image/default.png",
      text: "违章办理",
      url: "../peccancy/peccancy"
    }, {
      icon: "../../image/default.png",
      text: "代维修",
      url: "../chexianbj/chexianbj"
    }, {
      icon: "../../image/default.png",
      text: "代保养",
      url: "../chexianbj/chexianbj"
    }, {
      icon: "../../image/default.png",
      text: "接送机",
      url: "../airport/airport"
    }, {
      icon: "../../image/default.png",
      text: "代步车",
      url: "../stepcar/stepcar"
    },  {
      icon: "../../image/default.png",
      text: "代年检",
      url: "../yearaudit/yearaudit"
    }, {
      icon: "../../image/default.png",
      text: "其他",
      url: "../others/others"
    }],

    // 我的管家
    manager: {
      header: "../../image/default.png",
      name: "张伟",
      score: "9.0",
      level: 4.5,
      adept: "汽车挂牌；车辆保养、维修；续保、保险理赔等",
      detail: ""
    },
    message: []
  },

  onLoad: function() {
    app.editTabBar();
    //请求管家列表
    var that = this;
    wx.request({
      url: base.mainInfo,
      data: {
        id: 1
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      method: "POST",
      success: function(res) {
        console.log("首页信息");
        console.log(res.data.data);
        that.setData({
          mainInfo: res.data.data
        })

        wx.setStorageSync("services", that.data.mainInfo.services);
      }
    })



  },
  onShow: function() {
    var that = this;
    this.addListener();

    wx.request({
      url: base.getUserNoticeList,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        page: 0,
        size: 10,
        typeId: 1,
        flag: 0
      },
      method: "POST",
      success: function(res) {
        console.log(res.data.data);
        that.setData({
          'tips.list': res.data.data
        })

      }
    })
  },
  addListener: function() {
    var that = this;
    console.log("添加监听");
    app.globalData.callback = function(res) {
      console.log('收到服务器内容2：' + res.data)
      wx.setTabBarBadge({
        'index': 2,
        text: '1'
      })

      //解析消息 
      var json = JSON.parse(res.data);
      if (json.ntitle != null) {
        wx.showModal({
          title: json.ntitle,
          content: json.ncontent,
          success(e) {
            if (e.confirm) {
              if (json.isRead == "0") {
                wx.setStorageSync('orderConfirmType', 1);
                wx.setStorageSync('orderId', json.nid);
                wx.switchTab({
                  url: '../order/order',
                })
              } else {
                wx.setStorageSync('orderConfirmType', 1);
                wx.setStorageSync('orderId', json.nid);
                wx.navigateTo({
                  url: '../gjorder/gjorder',
                })
              }

            }
          }
        })
      } else {
        var msg = that.data.message;
        msg.push(json);
        that.setData({
          message: msg,
          scrollTop: 1000 * msg.length
        })
      }
    }

  },
  tapAction: function(e) {
    console.log(e.currentTarget.dataset.url);
    wx.reLaunch({
      url: e.currentTarget.dataset.url,
    })
  },
  tipBannerDetail: function(e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: "../../webview/webview?url=" + url
    })
  },
  tipDetail: function(e) {
    var item = e.currentTarget.dataset.item;
    wx.setStorageSync('notice', item);
    wx.navigateTo({
      url: '../../newsDetail/newsDetail',
    })
  },
  chatMessageAction: function(e) {
    var that = this;
    var data = that.data.message[that.data.message.length - 1];
    wx.setStorageSync("chatUser", data);
    wx.navigateTo({
      url: '../../msg/chat',
    })

    that.setData({
      message: []
    })

  }
})